package cn.edu.seu.ssny.controller.web;

import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.LoginLog;
import cn.edu.seu.ssny.entity.User;
import cn.edu.seu.ssny.service.*;
import cn.edu.seu.ssny.service.specification.SimpleSpecificationBuilder;
import cn.edu.seu.ssny.service.specification.SpecificationOperator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/web/log")
public class LogController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(LogController.class);


    @Autowired
    private IUserService userService;

    @Autowired
    private ILoginLogService loginLogService;


    /**
     * 功能描述:
     * <返回登录日志显示界面
     *
     * @return
     */
    @RequestMapping(value = {"/index"})
    public String index() {
        return "web/log/loginlog";
    }

    @RequestMapping(value = {"/login"}, method = RequestMethod.POST)
    @ResponseBody
    public Page<LoginLog> list(HttpSession session) throws ParseException {
        Enterprise enterprise = (Enterprise) session.getAttribute("enterprise");
        String startTimeSearch = request.getParameter("startTimeSearch");
        String endTimeSearch = request.getParameter("endTimeSearch");
        String keySearch = request.getParameter("keySearch"); //关键字，用户名
        Specification<LoginLog> specification = new Specification<LoginLog>() {
            @Override
            public Predicate toPredicate(Root<LoginLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicatesList = new ArrayList<>();
                if (StringUtils.isNotBlank(startTimeSearch)) {
                    predicatesList.add(cb.greaterThanOrEqualTo(root.get("loginTime"), startTimeSearch));
                }
                if (StringUtils.isNotBlank(endTimeSearch)) {
                    predicatesList.add(cb.lessThanOrEqualTo(root.get("loginTime"), endTimeSearch));
                }
                predicatesList.add(cb.equal(root.get("enterprise"), enterprise));
                if (StringUtils.isNotBlank(keySearch)) {
                    Join<LoginLog, User> joinUser = root.join("user", JoinType.INNER);
                    predicatesList.add(cb.like(joinUser.get("userName"), '%' + keySearch + '%'));
                }
                query.orderBy(cb.desc(root.get("loginTime")));
                Predicate[] predicates = new Predicate[predicatesList.size()];
                return cb.and(predicatesList.toArray(predicates));
            }
        };
        Page<LoginLog> page = loginLogService.findAll(specification, getPageRequest());
        return page;
    }

    /**
     * 功能描述:
     * <返回操作日志显示界面
     *
     * @return
     */
    @RequestMapping(value = {"/operationlog"})
    public String operationLogHtml() {
        return "web/log/operationlog";
    }

}
